package service

import (
	"context"
	"time"

	"Data-API-MCP/backend/internal/domain/entity"
	"Data-API-MCP/backend/internal/domain/repository"
)

// StatisticsService 统计服务
type StatisticsService struct {
	statsRepo repository.StatisticsRepository
}

// NewStatisticsService 创建统计服务
func NewStatisticsService(statsRepo repository.StatisticsRepository) *StatisticsService {
	return &StatisticsService{
		statsRepo: statsRepo,
	}
}

// RecordAPICall 记录API调用
func (s *StatisticsService) RecordAPICall(ctx context.Context, call *entity.APICall) error {
	return s.statsRepo.RecordAPICall(ctx, call)
}

// GetAPICallStats 获取API调用统计
func (s *StatisticsService) GetAPICallStats(ctx context.Context, startTime time.Time) (map[string]interface{}, error) {
	return s.statsRepo.GetAPICallStats(ctx, startTime)
}

// GetDatabaseStats 获取数据库连接统计
func (s *StatisticsService) GetDatabaseStats(ctx context.Context) (map[string]interface{}, error) {
	return s.statsRepo.GetDatabaseStats(ctx)
}

// GetServiceStats 获取服务统计
func (s *StatisticsService) GetServiceStats(ctx context.Context, serviceID string) (*entity.ServiceStats, error) {
	return s.statsRepo.GetServiceStats(ctx, serviceID)
}

// GetServiceHealth 获取服务健康状态
func (s *StatisticsService) GetServiceHealth(ctx context.Context, serviceID string) (map[string]interface{}, error) {
	return s.statsRepo.GetServiceHealth(ctx, serviceID)
}

// GetTopServices 获取热门服务
func (s *StatisticsService) GetTopServices(ctx context.Context, limit int) ([]*entity.ServiceStats, error) {
	return s.statsRepo.GetTopServices(ctx, limit)
}

// GetAPICallLogs 获取API调用日志
func (s *StatisticsService) GetAPICallLogs(ctx context.Context, apiID string, startTime, endTime time.Time) ([]*entity.APICall, error) {
	return s.statsRepo.GetAPICallLogs(ctx, apiID, startTime, endTime)
}

// RecordDatabaseConnectionStats 记录数据库连接统计
func (s *StatisticsService) RecordDatabaseConnectionStats(ctx context.Context, stats *entity.DatabaseConnectionStats) error {
	return s.statsRepo.RecordDatabaseConnectionStats(ctx, stats)
}

// GetDatabaseConnectionStats 获取数据库连接统计
func (s *StatisticsService) GetDatabaseConnectionStats(ctx context.Context, dbType string) ([]*entity.DatabaseConnectionStats, error) {
	return s.statsRepo.GetDatabaseConnectionStats(ctx, dbType)
}

// RecordServiceCallLog 记录服务调用日志
func (s *StatisticsService) RecordServiceCallLog(ctx context.Context, log *entity.ServiceCallLog) error {
	return s.statsRepo.RecordServiceCallLog(ctx, log)
}

// GetServiceCallLogs 获取服务调用日志
func (s *StatisticsService) GetServiceCallLogs(ctx context.Context, serviceID string, startTime, endTime time.Time) ([]*entity.ServiceCallLog, error) {
	return s.statsRepo.GetServiceCallLogs(ctx, serviceID, startTime, endTime)
}

// GetAPICallDetail 获取API调用详情
func (s *StatisticsService) GetAPICallDetail(ctx context.Context, id string) (*entity.APICall, error) {
	return s.statsRepo.GetAPICallDetail(ctx, id)
}

// GetAPIErrorStats 获取API错误统计
func (s *StatisticsService) GetAPIErrorStats(ctx context.Context, startTime time.Time) (map[string]interface{}, error) {
	return s.statsRepo.GetAPIErrorStats(ctx, startTime)
}

// GetAPIPerformance 获取API性能统计
func (s *StatisticsService) GetAPIPerformance(ctx context.Context, startTime time.Time) (map[string]interface{}, error) {
	return s.statsRepo.GetAPIPerformance(ctx, startTime)
}

// GetDBQueryStats 获取数据库查询统计
func (s *StatisticsService) GetDBQueryStats(ctx context.Context, startTime time.Time) (map[string]interface{}, error) {
	return s.statsRepo.GetDBQueryStats(ctx, startTime)
}

// GetDBPerformance 获取数据库性能统计
func (s *StatisticsService) GetDBPerformance(ctx context.Context) (map[string]interface{}, error) {
	return s.statsRepo.GetDBPerformance(ctx)
}

// GetDBConnectionStats 获取数据库连接统计
func (s *StatisticsService) GetDBConnectionStats(ctx context.Context) (map[string]interface{}, error) {
	return s.statsRepo.GetDBConnectionStats(ctx)
}

// GetSystemStatus 获取系统状态
func (s *StatisticsService) GetSystemStatus(ctx context.Context) (map[string]interface{}, error) {
	return s.statsRepo.GetSystemStatus(ctx)
}

// GetSystemResources 获取系统资源使用情况
func (s *StatisticsService) GetSystemResources(ctx context.Context) (map[string]interface{}, error) {
	return s.statsRepo.GetSystemResources(ctx)
}

// GetSystemLogs 获取系统日志
func (s *StatisticsService) GetSystemLogs(ctx context.Context, startTime, endTime time.Time) ([]*entity.SystemLog, error) {
	return s.statsRepo.GetSystemLogs(ctx, startTime, endTime)
}

// GetCPUUsage 获取CPU使用率
func (s *StatisticsService) GetCPUUsage(ctx context.Context) (float64, error) {
	// TODO: 实现获取CPU使用率的逻辑
	return 0.0, nil
}

// GetMemoryUsage 获取内存使用率
func (s *StatisticsService) GetMemoryUsage(ctx context.Context) (float64, error) {
	// TODO: 实现获取内存使用率的逻辑
	return 0.0, nil
}

// GetDiskUsage 获取磁盘使用率
func (s *StatisticsService) GetDiskUsage(ctx context.Context) (float64, error) {
	// TODO: 实现获取磁盘使用率的逻辑
	return 0.0, nil
}

// GetServicesHealth 获取服务健康状态
func (s *StatisticsService) GetServicesHealth(ctx context.Context) (map[string]interface{}, error) {
	// TODO: 实现获取服务健康状态的逻辑
	return map[string]interface{}{
		"status": "healthy",
	}, nil
}
